<?php
	class MySQL {
		var $connectionID;
		var $result;
		var $total;
		var $row;
		var $rowFields;
		
		function connect($URIObject=NULL){
			if($URIObject==NULL){
				import("net.URI");
				$URIObject = new URI(DB_DEFAULT);
			}
			$this->connectionID = @mysql_connect($URIObject->host,$URIObject->username,$URIObject->password);
			$connectionStatus = false;
			if($this->connectionID){
				$connectionStatus = $this->selectDB($URIObject->path);
			}
			return $connectionStatus;
		}
		function error(){
			//return mysql_error();
		}
		function disconnect(){
			//mysql_close($this->connectionID);
		}
		function selectDB($dbName){
			$selectDB = @mysql_select_db($dbName);
			return $selectDB;
		}
		
		function query($queryString,$countFound=false){
			//echo $queryString;
			$this->result = @mysql_query($queryString,$this->connectionID);
			if($countFound){
				$this->total = @mysql_result(@mysql_query("SELECT FOUND_ROWS();",$this->connectionId),0);
			}
			return $this->result;
		}
		function getTotal(){
			Logger::log("Database","SELECT FOUND_ROWS();");
			return $this->total = @mysql_result(@mysql_query("SELECT FOUND_ROWS();",$this->connectionId),0);
		}
		
		// Moves the cursor of the result to next row and return true if any result found
		function rowsLoop(){
			$this->row = @mysql_fetch_row($this->result);
			return ($this->row);
		}
		
		function getRow(){
		    $rowData = array();
		    for($f=0;$f<@mysql_num_fields($this->result);$f++){
			    $fieldName = @mysql_field_name($this->result,$f);
				$fieldTable = @mysql_field_table($this->result,$f);
			    $rowData[$fieldTable.".".$fieldName] = $this->row[$f];//htmlentities($this->row[$f]);
		    }
			return $rowData;
		}
		
		function buildDelete($query){
			$resultquery = "DELETE ";
			$resultquery .= " FROM ".$query->from;
			if($query->where!=NULL) $resultquery .= " WHERE ".$query->where;
			return $resultquery;
		}
		
		function buildSelect($query){
			$resultquery = "SELECT ";
			if($query->__processfounds__) $resultquery .= "SQL_CALC_FOUND_ROWS ";
			if($query->extendselect) $resultquery .= $query->extendselect.", ";
			$resultquery .= $query->select." FROM ".$query->from;
			if(count($query->join)>0){
				foreach($query->join as $join){
					$resultquery .= " ".$join[0]." JOIN ".$join[1]." ON ".$join[2];
				}
			}
			if($query->where) $resultquery .= " WHERE ".$query->where;
			if($query->group) $resultquery .= " GROUP BY ".$query->group;
			if($query->order) $resultquery .= " ORDER BY ".$query->order;
			if($query->limit) $resultquery .= " LIMIT ".$query->limit;
			return $resultquery;
		}
		
		function getLastID(){
			return @mysql_insert_id($this->connectionID);
		}
		
		function buildInsert($query){
			$resultquery = "INSERT ";
			if($query->extendedtype!=NULL) $resultquery .= $query->extendedtype;
			$resultquery .= " INTO ";
			$resultquery .= $query->from;
			if(is_string($query->keys)){
				$resultquery .= " (".$query->keys.")";
			}else if(is_array($query->keys)){
				$resultquery .= " (".join(",",$query->keys).")";
			}
			$resultquery .= " VALUES";
			if(is_string($query->values)){
				$resultquery .= " (".$query->values.")";
			}else if(is_array($query->values)){
				if($query->batchMode){
					$resultquery .= MySQL::__buildBatchValues__($query->values);
				}else{
					$resultquery .= " (".MySQL::__buildValues__($query->values).")";
				}
			}
			return $resultquery;
		}
		
		function buildUpdate($query){
			$resultquery = "UPDATE ";
			$resultquery .= $query->from;
			$resultquery .= " SET ";
			$resultquery .= MySQL::__buildPairValues__($query->keys,$query->values);
			if($query->where) $resultquery .= " WHERE ".$query->where;
			return $resultquery;
		}
		function __buildBatchValues__($valuesElemnts){
			$batchResult = array();
			foreach($valuesElemnts as $elmnt) {
				$values = $elmnt->__buildVars__();
				$result = " (";
				foreach($values as $key => $value) {
					if($result!=" (") $result.=", ";
					if(is_string($value)){
						$result .= "\"";
						$result .= addslashes($value);//(APP_USE_HTMLENTITIES) ? htmlentities($value) : $value ;
						$result .= "\"";
					}else{
						$result .= $value ;
					}
				}
				$result .= ")";
				$batchResult[] = $result;
			}
			return join(",",$batchResult);
		}
		function __buildValues__($valuesArray){
			$result = "";
			foreach($valuesArray as $key => $value) {
				if($result!="") $result.=", ";
				if(is_string($value)){
					$result .= "\"";
					$result .= addslashes($value);//(APP_USE_HTMLENTITIES) ? htmlentities($value) : $value ;
					$result .= "\"";
				}else{
					$result .= $value ;
				}
			}
			return $result;
		}
		
		function __buildPairValues__($keyArrays, $valuesArray){
			$result = "";
			foreach($keyArrays as $key) {
				$value = $valuesArray[$key];
				if($result!="") $result.=", ";
				if(is_string($value)){
					//".addslashes($value)."
					$result .= "$key = \"";
					$result .= $value;
					//(APP_USE_HTMLENTITIES) ? htmlentities($value) : $value ;
					//addslashes($value);//(APP_USE_HTMLENTITIES) ? htmlentities($value) : $value ;
					$result .= "\"";
				}else{
					$result .= "$key = ".$value;
				}
			}
			return $result;
		}
		
		/*
		function query(){
			mysql_query()
		}
		function lastID(){
			mysql_insert_id()
		}
		function loop(){
			
		}
		function disconect(){
			mysql_close();
		}*/
	}
?>
